package jobhunter.repository;

import java.util.List;

import jobhunter.model.Action;
import jobhunter.model.Contact;
import jobhunter.model.Process;
import jobhunter.security.SecurityContext;

import org.springframework.stereotype.Repository;


@Repository
@SuppressWarnings("unchecked")
public class ActionRepository extends GenericRepositoryJpa<Action> 
{		
	public ActionRepository() {
		setEntityClass(Action.class);
	}
	public List<Action> findByProcess(Process process)
	{
		return (List<Action>) em.createQuery("SELECT a FROM Action a WHERE a.process=:process AND a.user=:user ORDER BY a.actionDate desc")
				.setParameter("process", process)
				.setParameter("user", SecurityContext.getUser())
				.getResultList();
	}
	
	
	
	public List<Action> findByContact(Contact contact)
	{
		return (List<Action>) em.createQuery("SELECT a FROM Action a WHERE a.contact=:contact AND a.user=:user ORDER BY a.actionDate")
				.setParameter("contact", contact)
				.setParameter("user", SecurityContext.getUser())
				.getResultList();
	}
	
	@Override
	public Action merge(Action t) {
		if(t.getUser()==SecurityContext.getUser())
			return em.merge(t);
		else
			return null;
	}
	@Override
	public void remove(Action t) 
	{
		if(t.getUser()==SecurityContext.getUser())
			em.remove(t);		
	}
}
